<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" @submit.native.prevent size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="dictName">
        <el-input v-model.trim="queryParams.dictName" placeholder="请输入字典名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditItem = true" v-if="showBtns.add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100vh - 200px)">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="字典名称" min-width="160">
        <span slot-scope="{ row }">
          {{ row.dictName }}
        </span>
      </el-table-column>
      <el-table-column label="字典编码" prop="dictCode" min-width="70" />
      <el-table-column label="方法名称" align="center" min-width="70">
        <span slot-scope="{ row }">{{ row.funcName }}</span>
      </el-table-column>
      <el-table-column label="方法参数" align="center" min-width="200">
        <span slot-scope="{ row }">{{ row.paramJson }}</span>
      </el-table-column>
      <el-table-column label="所属工序" align="center" min-width="100">
        <span slot-scope="{ row }">{{ row.checktableName }}</span>
      </el-table-column>
      <el-table-column label="来源" align="center" min-width="70">
        <dict-tag slot-scope="{ row }" :options="dict.type.cq_pe_source_system" :value="row.sourceSystem" />
      </el-table-column>
      <el-table-column label="是否强制" align="center" min-width="70">
        <span slot-scope="{ row }">{{ row.strict ? '是' : '否' }}</span>
      </el-table-column>
      <!--<el-table-column label="创建人" prop="creatorName" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="150">
        <span slot-scope="{ row }">{{ row.createTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>-->
      <el-table-column label="修改人" prop="updatorName" align="center" min-width="80" />
      <el-table-column label="修改时间" prop="updateTime" align="center" width="120">
        <span slot-scope="{ row }">{{ row.updateTime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</span>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" width="140">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)" v-if="showBtns.edit">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="showBtns.remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-检查项版本
import { listFuncDict, delFuncDict } from '@/api/cqPe/funcDict'
import EditItem from './components/EditFuncDict'
import auth from '@/plugins/auth'
export default {
  name: 'FuncDict',
  components: { EditItem },
  dicts: ['cq_pe_source_system'],
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: ''
      },
      showEditItem: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const edit = auth.hasPermi(`cqPe:funcDict:edit`)
      const add = auth.hasPermi(`cqPe:funcDict:add`)
      const remove = auth.hasPermi(`cqPe:funcDict:delete`)
      return { edit, add, remove }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const res = await listFuncDict(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return
      const data = res.rows || []
      data.map(i => {
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.dictCode} - ${item.dictName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delFuncDict(item.funcDictId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
